##################################################################
# File : /lua/siminit.lua
# Description : This is the sim-specific top-level lua initialization file. It is run at initialization time
# 				to set up all lua state for the sim.
# Author : GPG
##################################################################

--[[
Initialization order within the sim:

  1. __blueprints is filled in from preloaded data

  2. simInit.lua [this file] runs. It sets up infrastructure necessary to make Lua classes work etc.

  if starting a new session:

    3a. ScenarioInfo is setup with info about the scenario

    4a. SetupSession() is called

    5a. Armies, brains, recon databases, and other underlying game facilities are created

    6a. BeginSession() is called, which loads the actual scenario data and starts the game

  otherwise (loading a old session):

    3b. The saved lua state is deserialized

--]]

function IsSim()
    return true
end

function IsUser()
    return false
end

function IsFrontEnd()
    return false
end

function IsRuleInit()
    return false
end

function IsEditor()
    return false
end

#
# Do global init and set up common global functions
#
doscript '/lua/globalInit.lua'

LOG('Active mods in sim: ', repr(__active_mods))

TicksPerSecond = 10

WaitTicks = coroutine.yield

function WaitSeconds(n)
    local ticks = math.max(1, n * TicksPerSecond)
    WaitTicks(ticks)
end

#
# Set up the sync table and some globals for use by scenario functions
#
doscript '/lua/SimSync.lua'

#
#SetupSession will be called by the engine after ScenarioInfo is set
#but before any armies are created.
#
function SetupSession()

    #LOG('SetupSession: ', repr(ScenarioInfo))

    ArmyBrains = {}

    #
    # ScenarioInfo is a table filled in by the engine with fields from the _scenario.lua
    # file we're using for this game. We use it to store additional global information
    # needed by our scenario.
    #
    ScenarioInfo.UnitNames = {}
    
    #
    # ScenarioInfo.Env is the environment that the save file and scenario script file
    # are loaded into.
    #
    # We set it up here with some default functions that can be accessed from the
    # scenario script.
    #
    ScenarioInfo.Env = import('/lua/scenarioEnvironment.lua')

    #
    # Load the scenario save and script files
    #
    # The save file creates a table named "Scenario" in ScenarioInfo.Env,
    # containing most of the save data. We'll copy it up to a top-level global.
    #
    LOG('Loading save file: ',ScenarioInfo.save)
    doscript('/lua/dataInit.lua')
    doscript(ScenarioInfo.save, ScenarioInfo.Env)

    Scenario = ScenarioInfo.Env.Scenario

    if DiskGetFileInfo(ScenarioInfo.script) then
        LOG('Loading script file: ',ScenarioInfo.script)
        doscript(ScenarioInfo.script, ScenarioInfo.Env)
    else
        WARN('Map script not found')
    end

    ResetSyncTable()
end

#
# Army Brains
#
# OnCreateArmyBrain() is called by then engine as the brains are created, and we
# use it to store off various useful bits of info.
#
# The global variable "ArmyBrains" contains an array of AI brains, one for each army.
#
function OnCreateArmyBrain(index, brain, name, nickname)
    #LOG(string.format("OnCreateArmyBrain %d %s %s",index,name,nickname))
    ArmyBrains[index] = brain
    ArmyBrains[index].Name = name
    ArmyBrains[index].Nickname = nickname
    ScenarioInfo.UnitNames[index] = {}

#     InitializeArmyAI(name)
    #LOG('*SCENARIO DEBUG: ON POP, ARMY BRAINS = ', repr(ArmyBrains))
end

function InitializePrebuiltUnits(name)
    ArmyInitializePrebuiltUnits(name)
end

#
# BeginSession will be called by the engine after the armies are created (but without
# any units yet) and we're ready to start the game. It's responsible for setting up
# the initial units and any other gameplay state we need.
#
function BeginSession()
    local focusarmy = GetFocusArmy()
    if focusarmy >= 0 and ArmyBrains[focusarmy] then
        LocGlobals.PlayerName = ArmyBrains[focusarmy].Nickname
    end

    # Pass ScenarioInfo into OnPopulate() and OnStart() for backwards compatibility
    ScenarioInfo.Env.OnPopulate(ScenarioInfo)
    ScenarioInfo.Env.OnStart(ScenarioInfo)
end

#
# OnPostLoad called after loading a saved game
#
function OnPostLoad()
end

doscript('/lua/sim/globals.lua')